package cn.tedu.knows.faq.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class TimeAspect {

    // 匹配service包中所有接口的所有方法即
    @Pointcut("execution(public * cn.tedu.knows.faq.service.*.*(..))")
    public void timer() {
    }

    @Around("timer()")
    public Object timeRecord(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();// 开始时间
        Object obj = joinPoint.proceed();       // 调用方法
        long end = System.currentTimeMillis();  // 结束时间
        long time=end-start;
        // 获得方法名
        String methodName=joinPoint.getSignature().getName();
        System.out.println(methodName+"用时"+time+"ms");
        return obj;
    }

}
